<style>
    table.movimentacao th th{font-size:10px; padding: 2px; border-top:none; color: silver;}
    table.simples th{font-size:12px; padding: 2px; border-top:none; color: black;}
</style>
<br/>
<div class="pull-right">
    <a href="default/documento/pesquisa"              class="btn" style="margin-right:30px;">Pesquisar documentos</a>
    <a href="default/documento/index/lista/simples"   class="btn <?php echo ($this->lista == 'simples') ? 'btn-success': '';?>">Lista simples</a>
    <a href="default/documento/index/lista/detalhada" class="btn <?php echo ($this->lista == 'detalhada') ? 'btn-success': '';?>">Lista detalhada</a>
</div>
<a href="documento/form/">Novo Documento</a><br/><br/>
<?php if (count($this->data)):?>
<?php switch ($this->lista) {
case 'detalhada':
    foreach($this->data as $data):?>
        <div class="documento well" style="padding:5px;">
            <img style="float:left; position: relative;" 
                src="<?php echo $this->ServerUrl().'/'.$this->BaseUrl();?>/images/documento.png" />
            <div style="margin-left: 40px;">
                <b><?php preg_match('/([0-9]{5})([0-9]{6})([0-9]{4})([0-9]{2})/', $data['protocolo'], $matches);
                    if($matches):
                         $protocolo = $matches[1].'.'.$matches[2].'/'.$matches[3].'-'.$matches[4];
                         echo $protocolo;
                    endif;
                ?></b>                
                <span style="margin-left:20px;">
                    <?php echo $data['tipo'].' - nº '.$data['numero'].'/'.$data['ano']; ?>
                    <?php echo ' - '.$data['origem']; ?>
                </span>

                <span class="pull-right" style="margin-left:50px;"><a href="documento/form/id/<?php echo $data->id; ?>">Editar</a> | 
                <a href="documento/delete/id/<?php echo $data->id; ?>">Excluir</a></span><br/>
                <div>
                    <div style="width:500px; float:left; margin-right: 20px;">
                        <span style="font-size:11px; color: silver; margin: 0px;">Assunto</span><br/>
                            <?php echo $data['assunto']; ?><br/>
                    </div>
                    <div style="float:left; width: 330px;">
                        <span style="font-size:11px; color: silver; margin: 0px;">Interessado</span><br/>
                        <?php echo $data['interessado']; ?>
                    </div>
                    <div style="clear:both;"></div>
                    <script>load('documento/movimentacao/doc_id/<?php echo $data['id'];?>');</script>
                    
                    <div class="well" style="padding:5px; margin:10px 10px 10px 0; background-color: white"  id="mov<?php echo $data['id'];?>">
                        <span  style="float:right; position: relative;">
                        <a class="cursor btnAdicionar" data-id ="<?php echo $data['id']?>" >
                            <img src="<?php echo $this->ServerUrl().$this->BaseUrl().'/images/adicionar.jpg'?>" />
                        </a>
                        </span>
                        <div>
                            <?php $this->atual = $data; echo $this->render('documento/movimentacao.phtml'); ?>
                        </div>
                    </div>
                </div>
            </div>

        </div>
		<?php endforeach;
break;
default:?>
    <table class="table table-condensed simples">
        <tr><th style="width:90px;">Dt Documento</th>
            <th style="width:170px;">Protocolo</th>
            <th style="width:230px;">Documento</th>
            <th>Assunto</th>
            <th style="width:50px;"></th>
        </tr>
    <?php foreach($this->data as $data):?>
            <tr>
                <td><?php echo $this->formataData($data['data']); ?></td>
                <td><?php preg_match('/([0-9]{5})([0-9]{6})([0-9]{4})([0-9]{2})/', $data['protocolo'], $matches);
                    if($matches):
                         $protocolo = $matches[1].'.'.$matches[2].'/'.$matches[3].'-'.$matches[4];
                         echo $protocolo;
                    endif; ?>
                </td>
                <td>
                    <a href="default/documento/detalhe/doc_id/<?php echo $data['id']; ?>">
                        <?php echo $data['numero'].'/'.$data['ano'].'-'.$data['origem'] ; ?>
                    </a>
                </td>
                <td><?php echo $data['assunto'];?></td>
                <td></td>
            </tr>
		<?php endforeach;?>
    </table>
<?php 
break;
}
else: ?>
    <br/><br/>
	<p class="alert alert-info">Não existem documentos cadastrados.</p>
<?php endif; ?>

    
<script>

botoes();

function botoes(){    

    $('.btnAdicionar').each(function(){
        $(this).removeClass('btnAdicionar');
        $(this).click(function(){
            var $doc = $(this);
            var $url = '<?php echo $this->ServerUrl().$this->baseUrl()?>/default/documento/form-movimentacao/doc_id/'+$doc.attr('data-id');
            $(this).parent().parent().find('.conteudo').load($url,null,function(){
                botoes();
            });
        });
    });
    
    $('.btnEditarMov').each(function(){
        $(this).removeClass('btnEditarMov');
        $(this).click(function(){
            var $mov = $(this);
            var $url = '<?php echo $this->ServerUrl().$this->baseUrl()?>/default/documento/form-movimentacao/id/'+$mov.attr('data-id');
            $(this).parent().parent().parent().parent().parent().find('.conteudo').load($url,null,function(){
                botoes();
            });
        });
    });
    
    $('.btnExcluirMov').each(function(){
        $(this).removeClass('btnExcluirMov');
        $(this).click(function(){
            var $mov = $(this);
            if (confirm('Deseja excluir a movimentação '+$mov.attr('data-texto')+'?')) {    
                var $url = '<?php echo $this->ServerUrl().$this->baseUrl()?>/default/documento/movimentacao-delete/id/'+$mov.attr('data-id');
                $(this).parent().parent().parent().load($url,null,function(){
                    botoes();
                });
            }
        });
    });
}
</script>
    